<?php
namespace Yzadmin\Controller;
use Think\Controller;
/**
* 计费管理
*/
class JifeiController extends CkController{
   
   
   //黑名单IP
   public function block_ip()
   {
      $M=M("block_ip");
      $count = $M->count(1);// 查询满足要求的总记录数
      $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
      $data['page'] = $Page->show();// 分页显示输出
      $data['log']=$M->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
      $data['title']="黑名单IP";
      $this->assign($data);
      $this->display();
   }
   
   
   //添加黑名单处理
   public function add_block_ip()
   {
      $data=I('post.');
      if(!is_ip($data['ip'])){ 
         $this->error("请输入合法的IP地址!");
      }
      $data['ip']= iplong($data['ip']);
      $data['time']=time();
      $data['num']=0;
      /*
      if($data['ip']< 0){
          $this->error("本地IP无需添加!");
      }
      */
      if(M("block_ip")->where(array("ip"=>$data['ip']))->find())
      {
         $this->error("IP已存在!请不要重复添加!");
      }
      if(M("block_ip")->add($data))
      {
         //$this->success("黑名单IP添加成功!");

      }else{
         $this->error("添加失败!");
      }
   }
   

   //删除黑名单IP
   public function del_block_ip()
   {
      $id=(int)I("get.id");
      if(M("block_ip")->where(array('id'=>$id))->delete()!==false)
      {
         $this->success("删除成功!");
      }else{
         $this->error("删除失败!");
      }
   }


   //拦截日志列表
   public function lanjie_log()
   {  
      $get=I('get.');
      $M=M("lanjieip_log");
      $act_start_time=strtotime($get['act_start_time']); //开始时间
      $act_stop_time=strtotime($get['act_stop_time']); //结束时间
      $where='';
      if($get['select'] && $get['nr']){
        $where = $get['select']."=".$get['nr'];
      }
      if($act_start_time && $act_stop_time)
      {
         if($where){ 
           $wherre= $where." and ";
         }
         $where =$wherre."time >=".$act_start_time." and ".'time <='.$act_stop_time;
      }
      $M->where($where);
      $count = $M->count(1);// 查询满足要求的总记录数
      $Page  = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
      $data['page'] = $Page->show();// 分页显示输出
      $data['where'] =$where;
      $data['log']=$M->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
      $data['title']="拦截日志";
      $this->assign($data);
      $this->display();
   }


   //按时间删除拦截日志
   public function del_lanjie_log()
   {
      $get=I('get.');
      $M=M("lanjieip_log");
      $act_start_time=strtotime($get['act_start_time']); //开始时间
      $act_stop_time=strtotime($get['act_stop_time']); //结束时间
      $where='';
      if($get['select'] && $get['nr']){
        $where = $get['select']."=".$get['nr'];
      }
      if($act_start_time && $act_stop_time)
      {
         if($where){ 
           $wherre= $where." and ";
         }
         $where =$wherre."time >=".$act_start_time." and ".'time <='.$act_stop_time;
      }
      if($coum=$M->where($where)->delete()!==false){ 
         $this->success("删除成功!");
      }else{ 
         $this->error("删除失败!");
      }

   }
   
   //计费IP日志
   public function jifei_log()
   {
      $get=I('get.');
      $M=D("Jifei");
      $act_start_time=strtotime($get['act_start_time']); //开始时间
      $act_stop_time=strtotime($get['act_stop_time']); //结束时间
      $where='';
      if($get['select'] && $get['nr']){
        $where = $get['select']."=".$get['nr'];
      }
      if($act_start_time && $act_stop_time)
      {
         if($where){ 
           $wherre= $where." and ";
         }
         $where =$wherre."jifeiip_log.time >=".$act_start_time." and ".'jifeiip_log.time <='.$act_stop_time;
      }

      $M->where($where);
      $count = M("jifeiip_log")->count(1);// 查询满足要求的总记录数
      $Page  = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
      $Page->setConfig('prev', "上一页");//上一页
      $Page->setConfig('next', '下一页');//下一页
      $Page->setConfig('first', '首页');//第一页
      $Page->setConfig('last', "末页");//最后一页
      $Page -> setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );

      $data['count'] = $count; //数据总和
      $data['page'] = $Page->show();// 分页显示输出
      $data['where'] =$where;
      $data['log']=$M->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
      $data['title']="计费日志";
      $this->assign($data);
      $this->display();
   }


   //按时间删除拦截日志
   public function del_jifei_log()
   {
      $get=I('get.');
      $M=M("jifeiip_log");
      $act_start_time=strtotime($get['act_start_time']); //开始时间
      $act_stop_time=strtotime($get['act_stop_time']); //结束时间
      $where='';
      if($get['select'] && $get['nr']){
        $where = $get['select']."=".$get['nr'];
      }
      if($act_start_time && $act_stop_time)
      {
         if($where){ 
           $wherre= $where." and ";
         }
         $where =$wherre."time >=".$act_start_time." and ".'time <='.$act_stop_time;
      }
      if($coum=$M->where($where)->delete()!==false){ 
         $this->success("删除成功!");
      }else{ 
         $this->error("删除失败!");
      }
    }

    //清空计费数据(先保存计费单IP周期的数据，然后在所有数据库在导入先有数据)
    public function clear_jifei_log()
    {
        $time =time()- C("JIFEI_LOG_TIME")*3600;
        echo $time; die;
        $data= M("jifeiip_log")->where("time >".$time)->select();
        P($data);
    }
    
  
    public function show()
      {
       $Model = M(); // 实例化一个model对象 没有对应任何数据表
       $data= $Model->query("SHOW TABLE STATUS FROM `weixinad` LIKE 'yz\_jifeiip\_log%'");
       p($data);
    }
   

   //计费IP日志
   public function jiakuan_log()
   {
      $get=I('get.');
      $M=D("Jiakuan")->relation(true);
      $act_start_time=strtotime($get['act_start_time']); //开始时间
      $act_stop_time=strtotime($get['act_stop_time']); //结束时间
      $where='';
      if($get['select'] && $get['nr']){
        $where = $get['select']."=".$get['nr'];
      }
      if($act_start_time && $act_stop_time)
      {
         if($where){ 
           $wherre= $where." and ";
         }
         $where =$wherre."time >=".$act_start_time." and ".'time <='.$act_stop_time;
      }
      $M->where($where);
      $count = M("jiakuan_log")->count(1);// 查询满足要求的总记录数
      $Page  = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
      $Page->setConfig('prev', "上一页");//上一页
      $Page->setConfig('next', '下一页');//下一页
      $Page->setConfig('first', '首页');//第一页
      $Page->setConfig('last', "末页");//最后一页
      $Page -> setConfig ( 'theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%' );
      $data['count'] = $count; //数据总和
      $data['page'] = $Page->show();// 分页显示输出
      $data['where'] =$where;
      $data['log']=$M->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
      $data['title']="加款日志";
      //P($data);die();
      $this->assign($data);
      $this->display();
   }

   //按推荐删除加款日志
   public function del_jiakuan_log()
   {
      $get=I('get.');
      $act_start_time=strtotime($get['act_start_time']); //开始时间
      $act_stop_time=strtotime($get['act_stop_time']); //结束时间
      $where='';
      if($get['select'] && $get['nr']){
        $where = $get['select']."=".$get['nr'];
      }
      if($act_start_time && $act_stop_time)
      {
         if($where){ 
           $wherre= $where." and ";
         }
         $where =$wherre."time >=".$act_start_time." and ".'time <='.$act_stop_time;
      }
      if(M("jiakuan_log")->where($where)->delete()!==false && M("jiakuan_xiaji_log")->where($where)->delete()!==false){ 
         $this->success("删除成功!");
      }else{ 
         $this->error("删除失败!");
      }
   }

   
   //扣量日志
   public function kouling_log()
   {
      $get=I('get.');
      $M=D("kouliang");
      $act_start_time=strtotime($get['act_start_time']); //开始时间
      $act_stop_time=strtotime($get['act_stop_time']); //结束时间
      $where='';
      if($get['select'] && $get['nr']){
        $where = $get['select']."=".$get['nr'];
      }
      if($act_start_time && $act_stop_time)
      {
         if($where){ 
           $wherre= $where." and ";
         }
         $where =$wherre."kouliangip_log.time >=".$act_start_time." and ".'kouliangip_log.time <='.$act_stop_time;
      }
      $M->where($where);
      $count = M("kouliangip_log")->count(1);// 查询满足要求的总记录数
      $Page  = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
      $Page->setConfig('prev', "上一页");//上一页
      $Page->setConfig('next', '下一页');//下一页
      $Page->setConfig('first', '首页');//第一页
      $Page->setConfig('last', "末页");//最后一页
      $Page -> setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
      $data['count'] = $count; //数据总和
      $data['page'] = $Page->show();// 分页显示输出
      $data['where'] =$where;
      $data['log']=$M->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
      $data['title']="扣量日志";
      //P($data);die();
      $this->assign($data);
      $this->display();
   }

   
    //按时间删除扣量日志
   public function del_kouliang_log()
   {
      $get=I('get.');
      $M=M("kouliangip_log");
      $act_start_time=strtotime($get['act_start_time']); //开始时间
      $act_stop_time=strtotime($get['act_stop_time']); //结束时间
      $where='';
      if($get['select'] && $get['nr']){
        $where = $get['select']."=".$get['nr'];
      }
      if($act_start_time && $act_stop_time)
      {
         if($where){ 
           $wherre= $where." and ";
         }
         $where =$wherre."time >=".$act_start_time." and ".'time <='.$act_stop_time;
      }
      if($coum=$M->where($where)->delete()!==false){ 
         $this->success("删除成功!");
      }else{ 
         $this->error("删除失败!");
      }
    }


}